
export const desc = {
    "name": "注册登录",
    "desc": "注册登录",
    "params_type": "json",
    "params": [
        {
            "en_name": "message_type",
            "cn_name": "消息类型",
            "desc": " 1-登录 1.1-注册",
            "example": 1
        },
        {
            "en_name": "username",
            "cn_name": "账号",
            "desc": "",
            "example": "123123123"
        },
        {
            "en_name": "password",
            "cn_name": "密码",
            "desc": "",
            "example": "123123123"
        },
    ],
    "returns": [],
    "example": 'JGESDK:发送(1,{"message_type":"1","username": "123123123","password":"123123123"})'
}

// @ts-ignore
export async function execute(client_id, message_json) {
    let message_type = message_json["message_type"];
    let username = message_json["username"];
    let password = message_json["password"];

    //登录
    if (message_type === 1) {
        if (username === undefined || username === "" || password === undefined || password === "") {
            ClientUtils.send_basic_prompt(client_id, "#R账户或密码不能为空!!!");
        }
        else {
            let accounts = await G.Accounts.query_accounts_db(username);
            if (accounts.length === 0) {
                ClientUtils.send_basic_prompt(client_id, "#R账号不存在!!!");
            }
            else if (G.Accounts.is_online(username)) {
                ClientUtils.send_basic_prompt(client_id, "#R该账号已登录!!!");
            }
            else {
                let accounts_json = JSON.parse(accounts);
                if (password !== accounts_json["password"]) {
                    ClientUtils.send_basic_prompt(client_id, "#R密码错误!!!");
                } else {
                    G.Accounts.login(client_id, accounts_json);
                    //加载角色
                    await G.Characters.load_characters(client_id, accounts_json["id"], username);
                }
            }
        }
    }
    //注册
    if (message_type === 1.1) {
        if (username === undefined || username === "" || password === undefined || password === "") {
            ClientUtils.send_basic_prompt(client_id, "#R账户或密码不能为空!!!");
        }
        else if (await G.Accounts.is_exsit(username)) {
            ClientUtils.send_basic_prompt(client_id, "#R账号已被注册!!!");
        } else {
            let id = await G.Accounts.register(username, password);
            if (Number(id) === 0) {
                ClientUtils.send_basic_prompt(client_id, "#R注册失败!!!");
            } else {
                ClientUtils.send_client_message(client_id, 3.1, "");
                ClientUtils.send_basic_prompt(client_id, "#Y注册成功!!!");
            }
        }
    }
}


